package language.advanced.functional;

public class function_first_class {

    interface Function {
        int opera(int a);
    }

    public static void main(String[] a) {
        Function identity = x->x;
        Function square = x->x*x;
        Function cube = x -> x*x*x;
        System.out.println(sum(identity, 1,10)); // return 55
        System.out.println(sum(square, 1,10)); // return 385
        System.out.println(sum(cube, 1,10));   // return 3025
    }

    public static int sum(Function term, int a, int b) {

        int sum = 0;
        for (int i = a; i <= b; i++) {
            sum += term.opera(i);
        }
        return sum;
    }
}
